import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:gw_github_flutter/common/global.dart';
import 'package:gw_github_flutter/common/notifier.dart';
import 'package:gw_github_flutter/common/route_name.dart';
import 'package:gw_github_flutter/generated/l10n.dart';
import 'package:gw_github_flutter/route/alertOrDialog/alertOrDialogRoute.dart';
import 'package:gw_github_flutter/route/systemWidget/button/button_route.dart';
import 'package:gw_github_flutter/route/flowAndWrap/WrapRoute_tags.dart';
import 'package:gw_github_flutter/route/flowAndWrap/wrapRoute.dart';
import 'package:gw_github_flutter/route/flowAndWrap/wrapRoute_flowCell.dart';
import 'package:gw_github_flutter/route/flowAndWrap/wrapRoute_flowTableView.dart';
import 'package:gw_github_flutter/route/home/homeRoute.dart';
import 'package:gw_github_flutter/route/language/languageRoute.dart';
import 'package:gw_github_flutter/route/launchScreen/intro_swiper_page.dart';
import 'package:gw_github_flutter/route/launchScreen/launchScreenPage.dart';
import 'package:gw_github_flutter/route/login/loginRoute.dart';
import 'package:gw_github_flutter/route/sliver/sliverRoute.dart';
import 'package:gw_github_flutter/route/sliver/sliverRoute_AZList.dart';
import 'package:gw_github_flutter/route/sliver/sliverRoute_stickyHeader.dart';
import 'package:gw_github_flutter/route/sliver/sliverRoute_appBar.dart';
import 'package:gw_github_flutter/route/sliver/sliverRoute_noAppBar.dart';
import 'package:gw_github_flutter/route/sliver/tableview/sliverRoute_sideHeader.dart';
import 'package:gw_github_flutter/route/sliver/tableview/sliverRoute_tableView.dart';
import 'package:gw_github_flutter/route/sliver/tableview/sliverRoute_tableView_appBar.dart';
import 'package:gw_github_flutter/route/sliver/tableview/sliverRoute_tableView_customSection.dart';
import 'package:gw_github_flutter/route/sliver/tableview/sliverRoute_tableView_tabbar.dart';
import 'package:gw_github_flutter/route/swiper/swiperRoute.dart';
import 'package:gw_github_flutter/route/systemWidget/checkBoxAndSwitch/checkBoxAndSwitchRoute.dart';
import 'package:gw_github_flutter/route/systemWidget/chip/chipRoute.dart';
import 'package:gw_github_flutter/route/systemWidget/image/imageViewRoute.dart';
import 'package:gw_github_flutter/route/systemWidget/notification/notificationRoute.dart';
import 'package:gw_github_flutter/route/systemWidget/slider/sliderRoute.dart';
import 'package:gw_github_flutter/route/systemWidget/systemRoute.dart';
import 'package:gw_github_flutter/route/systemWidget/textField/textFieldRoute.dart';
import 'package:gw_github_flutter/route/theme/themeRoute.dart';
import 'package:gw_github_flutter/route/tools/toolsRoute.dart';
import 'package:gw_github_flutter/route/tools/toolsRoute_imagePicker.dart';
import 'package:gw_github_flutter/tools/gwAutoSizeTool.dart';
import 'package:gw_github_flutter/tools/gwEasyRefrashManager.dart';
import 'package:gw_github_flutter/tools/refresh_glowindicator.dart';
import 'package:provider/provider.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

void main() {
  Global.init().then(
    (value) {
      runApp(const MyApp());
    },
  );
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    EasyRefresh.defaultHeaderBuilder =
        () => GwEasyRefrashManager.setRefrashHeader();
    EasyRefresh.defaultFooterBuilder =
        () => GwEasyRefrashManager.setRefrashFooter();
    WidgetsBinding.instance.addObserver(this);
  }

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (_) => ThemeModel()),
        ChangeNotifierProvider(create: (_) => UserModel()),
        ChangeNotifierProvider(create: (_) => LocaleModel()),
      ],
      child: Consumer2<ThemeModel, LocaleModel>(
        builder: (context, value, value2, child) {
          return GetMaterialApp(
            theme: ThemeData(
              primarySwatch: value.theme as MaterialColor,
            ),
            onGenerateTitle: (context) {
              return S.of(context).title;
            },
            debugShowCheckedModeBanner: false,
            locale: value2.getLocale(),
            supportedLocales: S.delegate.supportedLocales,
            localizationsDelegates: const [
              GlobalMaterialLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              S.delegate
            ],
            localeResolutionCallback: (locale, supportedLocales) {
              var gL = value2.getLocale();
              if (gL != null) {
                return gL;
              } else {
                Locale? _locale;
                if (supportedLocales.contains(locale)) {
                  _locale = locale;
                } else {
                  //如果系统语言不是中文简体或美国英语，则默认使用美国英语
                  _locale = Locale('en', 'US');
                }
                return _locale;
              }
            },
            initialRoute: RouteName.launchScreenPage,
            builder: (context, child) {
              Widget baseW = FlutterEasyLoading(child: child);
              baseW = MediaQuery(
                  data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                  child: baseW);
              // baseW = GwAutoSizeUtil.appBuilder(context, baseW);
              return baseW;
            },
            // EasyLoading.init(),
            getPages: _getPage(),
            // routes: {
            //   RouteName.login:(context) => LoginRoute(),
            //   RouteName.theme:(context) => ThemeRoute(),
            //   RouteName.language:(context) => LanguageRoute(),
            // },
            // home: HomeRoute(),
            // builder: (context, child) {
            //   Widget homeLoad = FlutterEasyLoading(child: child);
            //   return homeLoad;
            // },
          );
        },
      ),
    );
  }

  List<GetPage>? _getPage() {
    return [
      GetPage(name: RouteName.home, page: () => HomeRoute()),
      GetPage(name: RouteName.login, page: () => LoginRoute()),
      GetPage(name: RouteName.theme, page: () => ThemeRoute()),
      GetPage(name: RouteName.language, page: () => LanguageRoute()),
      GetPage(name: RouteName.textFieldRoute, page: () => TextFieldRoute()),
      GetPage(name: RouteName.buttonRoute, page: () => ButtonRoute()),
      GetPage(name: RouteName.alertOrDialogRoute, page: () => AlertOrDialogRoute()),
      GetPage(name: RouteName.intro_swiper_page, page: () => IntroSwiperPage()),
      GetPage(name: RouteName.launchScreenPage, page: () => LaunchScreenPage()),
      GetPage(name: RouteName.systemRoute, page: () => SystemRoute()),
      GetPage(name: RouteName.imageViewRoute, page: () => ImageViewRoute()),
      GetPage(name: RouteName.sliverRoute, page: () => SliverRoute()),
      GetPage(
          name: RouteName.sliverRoute_appBar, page: () => SliverRouteAppBar()),
      GetPage(
          name: RouteName.sliverRoute_noAppBar,
          page: () => SliverRouteNoAppBar()),
      GetPage(
          name: RouteName.sliverRoute_stickyHeader,
          page: () => SliverRouteStickyHeader()),
      GetPage(
          name: RouteName.sliverRoute_tableView,
          page: () => SliverRouteTableView()),
      GetPage(
          name: RouteName.sliverRoute_tableView_appBar,
          page: () => SliverRouteTableViewAppBar()),
      GetPage(
          name: RouteName.sliverRoute_tableView_customSection,
          page: () => SliverRouteTableViewCustomSection()),
      GetPage(
          name: RouteName.sliverRoute_tableView_tabbar,
          page: () => SliverRouteTableViewTabbar()),
      GetPage(
          name: RouteName.sliverRoute_sideHeader,
          page: () => SliverRouteSideHeader()),
      GetPage(
          name: RouteName.sliverRoute_AZList, page: () => SliverRouteAZList()),
      GetPage(name: RouteName.wrapRoute, page: () => WrapRoute()),
      GetPage(name: RouteName.wrapRoute_tags, page: () => WrapRouteTags()),
      GetPage(
          name: RouteName.wrapRoute_flowCell, page: () => WrapRouteFlowCell()),
      GetPage(
          name: RouteName.wrapRoute_flowTableView,
          page: () => WrapRouteFlowTableView()),
      GetPage(name: RouteName.swiperRoute, page: () => SwiperRoute()),
      GetPage(name: RouteName.toolsRoute, page: () => ToolsRoute()),
      GetPage(
          name: RouteName.toolsRoute_imagePicker,
          page: () => ToolsRouteImagePicker()),
      GetPage(name: RouteName.sliderRoute, page: () => SliderRoute()),
      GetPage(name: RouteName.chipRoute, page: () => ChipRoute()),
      GetPage(
          name: RouteName.checkBoxAndSwitchRoute,
          page: () => CheckBoxAndSwitchRoute()),
      GetPage(
          name: RouteName.notificationRoute, page: () => NotificationRoute()),
    ];
  }
}
